<?php
	class Assign extends MY_Controller{
		
		public function __construct(){
			parent::__construct();			
			$this->load->model('assign_model');
			$this->load->model('group_model');
			$this->load->model('module_model');
			
		}
		
		public function index(){
			
				$data['assign'] = $this->assign_model->getAssignList();
				$data['title'] = "Assign list";
				$this->load->view('templates/header1', $data);
				$this->load->view('assign/index', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
		public function displayForm($id=FALSE){
			
				$this->load->helper("form");
				$data = array();
				$data['title']="Assign Permission";
				// Get Group List
				$data['group'] = $this->group_model->getGroupList();
				
				// Get Module List
				$data['module'] = $this->module_model->getModuleList();
				
				// If Edit case
				if($id!=FALSE){
					// Get The assign item
					$data['assign'] = $this->assign_model->getAssignList($id);
					
					// Loop All Module Item and check on the item checked from 'assign'
					$data['module'] = $this->_checkedPermission($data['assign']['module_id'], $data['module']);				
					
					$this->load->view('templates/header1', $data);
					$this->load->view('assign/form_edit', $data);
					$this->load->view('templates/right', $data);
					$this->load->view('templates/footer');
				}
				else{	// Create new case
					$this->load->view('templates/header1', $data);
					$this->load->view('assign/form', $data);
					$this->load->view('templates/right', $data);
					$this->load->view('templates/footer');
				}	
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->assign_model->delete($id);
			redirect("assign", "refresh");
		}
		
		public function save(){
			
				$this->load->library('form_validation');
				$this->form_validation->set_rules('gid', 'Group', 'required');
				$this->load->helper("form");			
				if ($this->form_validation->run() === FALSE){
					$data['title']="Assign Permission";
					$this->load->view('templates/header1', $data);
					$this->load->view('assign/form', $data);
					$this->load->view('templates/right', $data);
					$this->load->view('templates/footer');
				}
				else{
					$this->assign_model->save();
					redirect("assign", "refresh");
				}
			
		}
		
		private function _checkedPermission($str_checked, $arr_module){
			if(strlen($str_checked) < 1 || count($arr_module)<1){
				return $arr_module;
			}
			$arr_checked = explode(",", $str_checked);
			if(count($arr_checked)<1)
				return $arr_module;
				
			foreach($arr_module as &$v){
				$id = $v['id'];
				foreach($arr_checked as $i){
					if($id==$i){
						$v['checked'] = 1;
						break;
					}
					else{
						$v['checked'] = 0;
						continue;
					}
				}
			}
			return $arr_module;
		}
		
	}
?>